package com.uzai.console.service.zombiefanpassive.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.*;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.zombiefanpassive.zombiefanpassivetask.ZombieFanPassiveFindByDeviceTypeDto;
import com.uzai.console.dto.zombiefanpassive.zombiefanpassivetask.ZombieFanPassiveTaskDto;
import com.uzai.console.dto.zombiefanpassive.zombiefanpassivetask.ZombieFanPassiveTaskIdDto;
import com.uzai.console.dto.zombiefanpassive.zombiefanpassivetask.ZombieFanPassiveTaskQuery;
import com.uzai.console.entity.*;
import com.uzai.console.jsonvo.zombiefanpassive.ZombieFanPassiveSendPara;
import com.uzai.console.mapper.ZombieFanPassiveMapper;
import com.uzai.console.mapper.ZombieFanPassiveTaskMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.zombiefanpassive.ZombieFanPassiveService;
import com.uzai.console.service.zombiefanpassive.ZombieFanPassiveTaskService;
import com.uzai.console.vo.zombiefanpassive.zombiefanpassivetask.ZombieFanPassiveTaskVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 僵尸粉任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class ZombieFanPassiveTaskServiceImpl implements ZombieFanPassiveTaskService {

    Logger logger = LoggerFactory.getLogger(ZombieFanPassiveTaskServiceImpl.class);

    @Autowired
    private ZombieFanPassiveTaskMapper zombieFanPassiveTaskMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ZombieFanPassiveMapper zombieFanPassiveMapper;
    @Autowired
    private ZombieFanPassiveService zombieFanPassiveService;

    /**
     * 查询被动僵尸粉任务列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveTaskQuery
     * @return null
     */
    public Page<ZombieFanPassiveTaskVo> findZombieFanPassiveTaskList(ZombieFanPassiveTaskQuery zombieFanPassiveTaskQuery){

        if(StringUtils.isBlank(zombieFanPassiveTaskQuery.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人类型不能为空");
        }

        Integer now = DateUtil.getNowTime();

        //新建返回到页面的LIST对象
        List<ZombieFanPassiveTaskVo> zombieFanPassiveTaskInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<ZombieFanPassiveTask> page = new Page<>();
        zombieFanPassiveTaskQuery.convert(page);
        List<ZombieFanPassiveTask> zombieFanPassiveTaskList = zombieFanPassiveTaskMapper.findByList(page, zombieFanPassiveTaskQuery);
        if(zombieFanPassiveTaskList != null){
            for (ZombieFanPassiveTask zombieFanPassiveTask : zombieFanPassiveTaskList){
                ZombieFanPassiveTaskVo zombieFanPassiveTaskInfoVo = new ZombieFanPassiveTaskVo();
                BeanUtils.copyProperties(zombieFanPassiveTask, zombieFanPassiveTaskInfoVo);

                //查询群发类型名称
                if(StringUtils.isNotBlank(zombieFanPassiveTask.getDeviceType())){
                    DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getByKey(zombieFanPassiveTask.getDeviceType());
                    if(deviceTypeEnum != null){
                        zombieFanPassiveTaskInfoVo.setDeviceTypeName(deviceTypeEnum.getRemark());
                    }
                }
                zombieFanPassiveTaskInfoVoList.add(zombieFanPassiveTaskInfoVo);
            }
        }
        //返回到页面分页对象
        Page<ZombieFanPassiveTaskVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(zombieFanPassiveTaskInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询被动僵尸粉任务详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveTaskIdDto
     * @return null
     */
    public ZombieFanPassiveTaskVo findZombieFanPassiveTask(ZombieFanPassiveTaskIdDto zombieFanPassiveTaskIdDto){
        //返回到页面的VO对象
        ZombieFanPassiveTaskVo zombieFanPassiveTaskInfoVo = new ZombieFanPassiveTaskVo();

        if(zombieFanPassiveTaskIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanPassiveTask zombieFanPassiveTask = zombieFanPassiveTaskMapper.selectById(zombieFanPassiveTaskIdDto.getId(), zombieFanPassiveTaskIdDto.getMerId());

        if(zombieFanPassiveTask == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //将查询到的数据复制到页面VO中
        BeanUtils.copyProperties(zombieFanPassiveTask, zombieFanPassiveTaskInfoVo);

        //查询群发类型名称
        if(StringUtils.isNotBlank(zombieFanPassiveTask.getDeviceType())){
            DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getByKey(zombieFanPassiveTask.getDeviceType());
            if(deviceTypeEnum != null){
                zombieFanPassiveTaskInfoVo.setDeviceTypeName(deviceTypeEnum.getRemark());
            }
        }

        //机器人列表
        if(StringUtils.isNotBlank(zombieFanPassiveTask.getDeviceUniqueIds())){
            try {
                List<Long> deviceUniqueIdList = JSONObject.parseArray(zombieFanPassiveTask.getDeviceUniqueIds(), Long.class);
                zombieFanPassiveTaskInfoVo.setDeviceUniqueIdList(deviceUniqueIdList);
            }catch (Exception e){

            }
        }

        //发送参数
        if(StringUtils.isNotBlank(zombieFanPassiveTask.getSendPara())){
            try {
                ZombieFanPassiveSendPara sendPara = JSONObject.parseObject(zombieFanPassiveTask.getSendPara(), ZombieFanPassiveSendPara.class);
                zombieFanPassiveTaskInfoVo.setSendPara(sendPara);
            }catch (Exception e){

            }
        }

        return  zombieFanPassiveTaskInfoVo;
    }

    /**
     * 查询清理僵尸粉任务详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveFindByDeviceTypeDto
     * @return null
     */
    public ZombieFanPassiveTaskVo findZombieFanPassiveTaskByDevice(ZombieFanPassiveFindByDeviceTypeDto zombieFanPassiveFindByDeviceTypeDto){
        //返回到页面的VO对象
        ZombieFanPassiveTaskVo zombieFanPassiveTaskInfoVo = new ZombieFanPassiveTaskVo();

        if(StringUtils.isBlank(zombieFanPassiveFindByDeviceTypeDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        ZombieFanPassiveTask zombieFanPassiveTask = zombieFanPassiveTaskMapper.selectByDeviceType(zombieFanPassiveFindByDeviceTypeDto.getDeviceType(), zombieFanPassiveFindByDeviceTypeDto.getMerId());

        if(zombieFanPassiveTask == null ){
            return  zombieFanPassiveTaskInfoVo;
        }

        //将查询到的数据复制到页面VO中
        BeanUtils.copyProperties(zombieFanPassiveTask, zombieFanPassiveTaskInfoVo);

        //查询群发类型名称
        if(StringUtils.isNotBlank(zombieFanPassiveTask.getDeviceType())){
            DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getByKey(zombieFanPassiveTask.getDeviceType());
            if(deviceTypeEnum != null){
                zombieFanPassiveTaskInfoVo.setDeviceTypeName(deviceTypeEnum.getRemark());
            }
        }

        //机器人列表
        if(StringUtils.isNotBlank(zombieFanPassiveTask.getDeviceUniqueIds())){
            try {
                List<Long> deviceUniqueIdList = JSONObject.parseArray(zombieFanPassiveTask.getDeviceUniqueIds(), Long.class);
                zombieFanPassiveTaskInfoVo.setDeviceUniqueIdList(deviceUniqueIdList);
            }catch (Exception e){

            }
        }

        //发送参数
        if(StringUtils.isNotBlank(zombieFanPassiveTask.getSendPara())){
            try {
                ZombieFanPassiveSendPara sendPara = JSONObject.parseObject(zombieFanPassiveTask.getSendPara(), ZombieFanPassiveSendPara.class);
                zombieFanPassiveTaskInfoVo.setSendPara(sendPara);
            }catch (Exception e){

            }
        }

        return  zombieFanPassiveTaskInfoVo;
    }

    /**
     * 添加被动僵尸粉任务
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveTaskDto
     * @return null
     */
    public void addZombieFanPassiveTask(ZombieFanPassiveTaskDto zombieFanPassiveTaskDto){

        if(StringUtils.isBlank(zombieFanPassiveTaskDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人类型不能为空");
        }

        if(zombieFanPassiveTaskDto.getDeviceUniqueIdList() == null || zombieFanPassiveTaskDto.getDeviceUniqueIdList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择机器人");
        }

        if(StringUtils.isBlank(zombieFanPassiveTaskDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }

        ZombieFanPassiveSendPara sendPara = zombieFanPassiveTaskDto.getSendPara();
        if(sendPara == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入发送参数");
        }

        if(zombieFanPassiveTaskDto.getDeviceUniqueIdList() == null || zombieFanPassiveTaskDto.getDeviceUniqueIdList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入机器人");
        }

        if(zombieFanPassiveTaskDto.getRateMin() == null || zombieFanPassiveTaskDto.getRateMax() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入添加频率");
        }

        if(zombieFanPassiveTaskDto.getRateMin() > zombieFanPassiveTaskDto.getRateMax()){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "频率最小值不能大于频率最大值");
        }

        if(zombieFanPassiveTaskDto.getLimitTimeStart() == null || zombieFanPassiveTaskDto.getLimitTimeEnd() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入限制时间范围");
        }

        if(zombieFanPassiveTaskDto.getSendDayLimit() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入每日添加上限");
        }

        int now = DateUtil.getNowTime();
        ZombieFanPassiveTask zombieFanPassiveTask = new ZombieFanPassiveTask();
        zombieFanPassiveTask.setId(IdWorker.getId());
        zombieFanPassiveTask.setMerId(zombieFanPassiveTaskDto.getMerId());
        zombieFanPassiveTask.setDeviceType(zombieFanPassiveTaskDto.getDeviceType());
        zombieFanPassiveTask.setDeviceUniqueIds(JSONObject.toJSONString(zombieFanPassiveTaskDto.getDeviceUniqueIdList()));
        zombieFanPassiveTask.setSendPara(JSONObject.toJSONString(sendPara));
        zombieFanPassiveTask.setName(zombieFanPassiveTaskDto.getName());
        zombieFanPassiveTask.setRateMin(zombieFanPassiveTaskDto.getRateMin());
        zombieFanPassiveTask.setRateMax(zombieFanPassiveTaskDto.getRateMax());
        zombieFanPassiveTask.setLimitTimeStart(zombieFanPassiveTaskDto.getLimitTimeStart());
        zombieFanPassiveTask.setLimitTimeEnd(zombieFanPassiveTaskDto.getLimitTimeEnd());
        zombieFanPassiveTask.setSendDayLimit(zombieFanPassiveTaskDto.getSendDayLimit());
        zombieFanPassiveTask.setCreateTime(now);
        zombieFanPassiveTask.setUpdateTime(now);
        zombieFanPassiveTaskMapper.insertSelective(zombieFanPassiveTask);

    }

    /**
     * 删除被动僵尸粉任务
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveTaskIdDto
     * @return null
     */
    public void deleteZombieFanPassiveTask(ZombieFanPassiveTaskIdDto zombieFanPassiveTaskIdDto){

        if(zombieFanPassiveTaskIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        ZombieFanPassiveTask zombieFanPassiveTask = zombieFanPassiveTaskMapper.selectById(zombieFanPassiveTaskIdDto.getId(), zombieFanPassiveTaskIdDto.getMerId());

        if(zombieFanPassiveTask == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //删除任务
        zombieFanPassiveTaskMapper.deleteById(zombieFanPassiveTask.getId(), zombieFanPassiveTaskIdDto.getMerId());

    }

    /**
     * 修改被动僵尸粉任务
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param zombieFanPassiveTaskDto
     * @return null
     */
    public void updateZombieFanPassiveTask(ZombieFanPassiveTaskDto zombieFanPassiveTaskDto){

        if(StringUtils.isBlank(zombieFanPassiveTaskDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "机器人类型不能为空");
        }

        if(zombieFanPassiveTaskDto.getDeviceUniqueIdList() == null || zombieFanPassiveTaskDto.getDeviceUniqueIdList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择机器人");
        }

        if(StringUtils.isBlank(zombieFanPassiveTaskDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }

        ZombieFanPassiveSendPara sendPara = zombieFanPassiveTaskDto.getSendPara();
        if(sendPara == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入发送参数");
        }

        if(zombieFanPassiveTaskDto.getDeviceUniqueIdList() == null || zombieFanPassiveTaskDto.getDeviceUniqueIdList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入机器人");
        }

        if(zombieFanPassiveTaskDto.getRateMin() == null || zombieFanPassiveTaskDto.getRateMax() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入添加频率");
        }

        if(zombieFanPassiveTaskDto.getRateMin() > zombieFanPassiveTaskDto.getRateMax()){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "频率最小值不能大于频率最大值");
        }

        if(zombieFanPassiveTaskDto.getLimitTimeStart() == null || zombieFanPassiveTaskDto.getLimitTimeEnd() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入限制时间范围");
        }

        if(zombieFanPassiveTaskDto.getSendDayLimit() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入每日添加上限");
        }

        if(zombieFanPassiveTaskDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        ZombieFanPassiveTask zombieFanPassiveTask = zombieFanPassiveTaskMapper.selectById(zombieFanPassiveTaskDto.getId(), zombieFanPassiveTaskDto.getMerId());
        if(zombieFanPassiveTask == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }
        int now = DateUtil.getNowTime();
        zombieFanPassiveTask.setDeviceType(zombieFanPassiveTaskDto.getDeviceType());
        zombieFanPassiveTask.setDeviceUniqueIds(JSONObject.toJSONString(zombieFanPassiveTaskDto.getDeviceUniqueIdList()));
        zombieFanPassiveTask.setSendPara(JSONObject.toJSONString(sendPara));
        zombieFanPassiveTask.setName(zombieFanPassiveTaskDto.getName());
        zombieFanPassiveTask.setRateMin(zombieFanPassiveTaskDto.getRateMin());
        zombieFanPassiveTask.setRateMax(zombieFanPassiveTaskDto.getRateMax());
        zombieFanPassiveTask.setLimitTimeStart(zombieFanPassiveTaskDto.getLimitTimeStart());
        zombieFanPassiveTask.setLimitTimeEnd(zombieFanPassiveTaskDto.getLimitTimeEnd());
        zombieFanPassiveTask.setSendDayLimit(zombieFanPassiveTaskDto.getSendDayLimit());
        zombieFanPassiveTask.setUpdateTime(now);
        zombieFanPassiveTaskMapper.updateById(zombieFanPassiveTask);
    }

    /**
     * 自动添加被动僵尸粉
     * @author lucher
     * @date 2022/9/19 14:05
     * @param zombieFanPassiveTask
     */
    public void autoAddZombieFanPassive(ZombieFanPassiveTask zombieFanPassiveTask){

        if(zombieFanPassiveTask == null){
            logger.error("--添加被动僵尸粉任务不存在--");
           return;
        }

        if(StringUtils.isBlank(zombieFanPassiveTask.getSendPara())){
            logger.error("--该被动僵尸粉未配置发送参数--");
            return;
        }

        //查询最新未请求的被动僵尸粉
        ZombieFanPassive zombieFanPassive = zombieFanPassiveMapper.selectNewestAndStatus(zombieFanPassiveTask.getMerId(), 0);
        if(zombieFanPassive == null){
            return;
        }

        //添加被动僵尸粉
        zombieFanPassiveService.dealZombieFanPassiveSendMsg(zombieFanPassiveTask, zombieFanPassive);

    }






}
